/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sanerzone.smscenter.modules.sms.web;

import com.sanerzone.jeebase.common.config.Global;
import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.jeebase.common.web.BaseController;
import com.sanerzone.smscenter.biz.cache.TemplateRouteCachHelper;
import com.sanerzone.smscenter.biz.iface.TemplateRouteInterface;
import com.sanerzone.smscenter.modules.sms.entity.SmsTemplateRoute;
import com.sanerzone.smscenter.modules.sms.service.SmsTemplateRouteService;
import com.sanerzone.smscenter.biz.entity.TemplateRoute;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 网关模板管理Controller
 * @author jianr.z
 * @version 2018-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/smsTemplateRoute")
public class SmsTemplateRouteController extends BaseController{

	@Autowired
	private SmsTemplateRouteService smsTemplateRouteService;

	@Autowired
	private TemplateRouteInterface templateRouteInterface;
	@ModelAttribute
	public SmsTemplateRoute get(@RequestParam(required=false) String id) {
		SmsTemplateRoute entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = smsTemplateRouteService.get(id);
		}
		if (entity == null){
			entity = new SmsTemplateRoute();
		}
		return entity;
	}
	
	@RequiresPermissions("sms:smsTemplateRoute:view")
	@RequestMapping(value = {"list", ""})
	public String list(SmsTemplateRoute smsTemplateRoute, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SmsTemplateRoute> page = smsTemplateRouteService.findPage(new Page<SmsTemplateRoute>(request, response), smsTemplateRoute);
		model.addAttribute("page", page);
		return "modules/sms/smsTemplateRouteList";
	}

	@RequiresPermissions("sms:smsTemplateRoute:view")
	@RequestMapping(value = "form")
	public String form(SmsTemplateRoute smsTemplateRoute, Model model) {
		model.addAttribute("smsTemplateRoute", smsTemplateRoute);
		return "modules/sms/smsTemplateRouteForm";
	}

	@RequiresPermissions("sms:smsTemplateRoute:edit")
	@RequestMapping(value = "save")
	public String save(SmsTemplateRoute smsTemplateRoute, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, smsTemplateRoute)){
			return form(smsTemplateRoute, model);
		}

		smsTemplateRouteService.save(smsTemplateRoute);
		addMessage(redirectAttributes, "保存网关模板成功");
		TemplateRoute templateRoute=new TemplateRoute();
		templateRoute.setGatewayGroupId( Integer.parseInt( smsTemplateRoute.getGatewayGroupId()) );
		templateRoute.setTemplateContent( smsTemplateRoute.getTemplateContent() );
		templateRoute.setType( smsTemplateRoute.getType() );
		//刷新一遍缓存
		templateRouteInterface.addTemplateRouteToCach( templateRoute );
		return "redirect:"+ Global.getAdminPath()+"/sms/smsTemplateRoute/?repage";
	}
	
	@RequiresPermissions("sms:smsTemplateRoute:edit")
	@RequestMapping(value = "delete")
	public String delete(SmsTemplateRoute smsTemplateRoute, RedirectAttributes redirectAttributes) {
		smsTemplateRouteService.delete(smsTemplateRoute);
		addMessage(redirectAttributes, "删除网关模板成功");
		TemplateRoute templateRoute=new TemplateRoute();
		templateRoute.setGatewayGroupId( Integer.parseInt( smsTemplateRoute.getGatewayGroupId()) );
		templateRoute.setTemplateContent( smsTemplateRoute.getTemplateContent() );
		templateRoute.setType( smsTemplateRoute.getType() );
		templateRouteInterface.deleteTemplateRoute( templateRoute );
		return "redirect:"+Global.getAdminPath()+"/sms/smsTemplateRoute/?repage";
	}

}